<template>
  <div>
    <div class="header">
      <img src="../assets/meituan.png" alt="">
    </div>
    <div class="body-content">
      <div class="left-content">
        <img src="	https://s0.meituan.net/bs/file/?f=fe-sso-fs:build/page/static/banner/www.jpg" alt="">
      </div>
      <div class="right-content login-form">
        <div class="form-header">
          <span>账号登录</span>
        </div>
        <div class="form-content">
          <input type="text" name="username" placeholder="输入手机号" v-model="username">
          <input type="password" name="password" placeholder="输入密码" v-model="password">
          <div class="forget" @click="()=>this.$router.push({path: 'register',query: {message:'reset'}})">忘记密码?</div>
          <button type="" class="login-btn" @click="handleLogin">登录</button>
          <button type="" class="register-btn" @click="()=>this.$router.push({name:'register',query: {message:'register'}})">注册</button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    handleLogin() {
      let userNameReg = /^1[345789]\d{9}$/
      let passwordReg = /^.*(?=.{6,16})(?=.*\d)(?=.*[a-z]||[A-Z]).*$/
      if (!userNameReg.test(this.username)) {
        alert('请输入正确的手机号')
      } else if (!passwordReg.test(this.password)) {
        alert('请输入正确格式的密码')
      } else {
        // 记住以后要学着封装axios！
        this.request({
          url: 'login',
          method: 'GET',
          params: {
            username: this.username,
            password: this.password
          }
        }).then(res => {
          console.log(res)
          if (res.data.validateRes) {
            this.$store.commit('setUserInfo',this.username)
            localStorage.setItem('username',this.username)
            this.$router.push('/select')
          } else {
            alert('密码错误！')
          }
        })
      }
    }
  },
  mounted() {
    if (this.$route.query) {
      this.username = this.$route.query.username
    }
  }
}
</script>

<style lang="less" scoped>
.header {
  width: 980px;
  height: 47px;
  margin: 40px auto;
  text-align: left;
  img {
    width: 130px;
  }
}
.body-content {
  font-size: 14px;
  color: #666;
  width: 980px;
  margin: 0 auto 70px;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  .left-content {
  }
  .right-content {
    width: 270px;
    margin: 0 auto;

    .form-header {
      text-align: left;
      margin-top: 50px;
    }
    .form-content {
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: flex-start;
      input {
        width: 270px;
        height: 30px;
        margin: 8px 0;
        border-radius: 10px;
        border: 1px solid #ccc;
        outline: none;
        padding: 5px;
      }
      .forget {
        width: 100%;
        text-align: right;
        color: #fe8c00;
        font-size: 12px;
        cursor: pointer;
        &:hover {
          opacity: 0.7;
        }
      }
      .login-btn {
        width: 100%;
        height: 33px;
        margin: 20px 0 10px 0;
        background-image: linear-gradient(135deg, #ffd000 0, #ffbd00 100%);
        border-width: 0;
        box-shadow: 0 2px 1px rgb(191 105 0 / 15%);
        background-size: 100%;
        border-radius: 10px;
        font-weight: bold;
        cursor: pointer;
        &:hover {
          opacity: 0.9;
        }
      }
      .register-btn {
        width: 100%;
        height: 33px;
        background-size: 100%;
        background-image: linear-gradient(135deg, #ffd000 0, #ffbd00 100%);
        border-radius: 10px;
        border: 0;
        font-weight: bold;
        cursor: pointer;
        &:hover {
          opacity: 0.9;
        }
      }
    }
  }
}
</style>